package love.yuanmengrobot.qianyugarden.service.impl;

import love.yuanmengrobot.qianyugarden.dao.UserDao;
import love.yuanmengrobot.qianyugarden.entity.User;
import love.yuanmengrobot.qianyugarden.service.UserService;
import love.yuanmengrobot.qianyugarden.utils.ResultObj;
import love.yuanmengrobot.qianyugarden.utils.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * (User)表服务实现类
 *
 * @author zjj
 * @since 2021-07-13 14:28:15
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param objectid 主键
     * @return 实例对象
     */
    @Override
    public User queryById(String objectid) {
        return this.userDao.queryById(objectid);
    }

    @Override
    public User queryByLoginNameAndPassword(User user) {
        return this.userDao.queryByLoginNameAndPassword(user);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getObjectid());
    }

    /**
     * 通过主键删除数据
     *
     * @param objectid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String objectid) {
        return this.userDao.deleteById(objectid) > 0;
    }


    @Override
    public boolean updateHeadportrait(MultipartFile headportrait,User user){
        int update=0;
        try {
            String savePath = ResourceUtils.getURL("classpath:").getPath()+"static/img/userHeadportrait/";
            File savePathFile = new File(savePath);
            if(!savePathFile.exists()){
                savePathFile.mkdir();
            }
            String suffix = headportrait.getOriginalFilename().substring(headportrait.getOriginalFilename().lastIndexOf(".") + 1, headportrait.getOriginalFilename().length());
            String filename = UUID.randomUUID().toString().replaceAll("-","") + "." + suffix;
            System.out.println(savePath + filename);
            headportrait.transferTo(new File(savePath+filename));
            user.setHeadportrait("/img/userHeadportrait/"+filename);
            update = userDao.update(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return update>0;
    }
}
